<template>
  <el-container class="home-container">
    <!-- 标题部分 -->
    <el-header>
      <div>
        <img src="@/assets/logo.png" alt="" />
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体部分 -->
    <el-container>
      <!-- 左侧部分 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 回收按钮 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#313743"
          text-color="#fff"
          active-text-color="#278cff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 一级菜单的模块 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconListObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单的模块 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧部分 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script type="text/ecmascript-6">
export default {
  name:'',
  data() {
    return {
      menuList:[],
      iconListObj:{
        '125':'iconfont icon-customer',
        '103':'iconfont icon-quanxian',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-dingdan',
        '145':'iconfont icon-icon_huabanfuben',
      },
      isCollapse:false,
      activePath:'',
    }
  },
  components: {

  },
  created(){
    this.getMenuList();
    this.activePath=window.sessionStorage.getItem('activePath');
  },
  methods:{
    //退出功能
    logout(){
      window.sessionStorage.clear();
      this.$router.push('/login');
    },
    //获取左侧菜单的数据
    async getMenuList(){
      const{data:res} =await this.$http.get('menus');
      if(res.meta.status!=200) return this.$message.error(res.meta.msg);
      this.menuList=res.data;
    },
    //回收左侧菜单
    toggleCollapse(){
      this.isCollapse=!this.isCollapse;
    },
    //点击左侧二级按钮显示高亮
    saveNavState(activePath){
      //将地址储存到会话存储
      window.sessionStorage.setItem('activePath',activePath);
      this.activePath=activePath;
      // console.log(this.activePath);
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #363d40;
  display: flex;
  justify-content: space-between;
  font-size: 20px;
  color: #ccc;
  align-items: center;
  padding-left: 0;
  div {
    display: flex;
    align-items: center;
    img {
      width: 70px;
      height: 70px;
    }
  }
}
.el-aside {
  background-color: #313743;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #e9edf1;
}
.iconfont {
  padding-right: 15px;
}
.toggle-button {
  background-color: #495066;
  text-align: center;
  font-size: 10px;
  line-height: 24px;
  color: #ccc;
  letter-spacing: 0.2em; //字距
  cursor: pointer; //鼠标样式
}
</style>
